package com.jsh.erp.utils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * @author wy
 * @title: CollectionCombine
 * @projectName JSH_ERP
 * @description:
 * @date 2023-04-03 16:18
 */
public class CollectionCombineUtils {

    public static <T> List<T> CollectionCombines(List<List<T>> ll) throws Exception{
        if(ll==null) {
            throw new Exception("列表不能为null");
        }else if(ll.size()==0) {
            throw new Exception("列表不能为空");
        }else if(ll.size()==1) {
            return ll.get(0);
        }else {
            List tepmList = new ArrayList();
            for(int i = 0;i<ll.size()-1;i++) {
                tepmList = CollectionCombine2(ll.get(i),ll.get(i+1));
                if(i != ll.size()-1) {
                    ll.get(i+1).clear();
                    ll.get(i+1).addAll(tepmList);
                }
            }
            return tepmList;
        }
    }

    public static <T> List<T> CollectionCombine2(List<T> l1,List<T> l2) {
        List tepmList = new ArrayList();
        for(int i=0;i<l1.size();i++) {
            for(int j=0;j<l2.size();j++) {
                List aa = new ArrayList();
                if(l1.get(i) instanceof Collection) {
                    aa.addAll((Collection)l1.get(i));
                }else {
                    aa.add(l1.get(i));
                }

                aa.add(l2.get(j));
                tepmList.add(aa);
            }
        }

        return tepmList;
    }

    public static void main(String[] args) {
        List l1 = new ArrayList<Integer>();
        l1.add(1);
        l1.add(2);

        List l2 = new ArrayList<Integer>();
        l2.add(3);
        l2.add(4);

        List<List<Integer>> ll  = new ArrayList<List<Integer>>();
        ll.add(l1);
        ll.add(l2);

        try {
            List list = CollectionCombines(ll);

            System.out.println("=====输出组合后的列表=====");
            for(int i=0;i<list.size();i++) {
                System.out.println(list.get(i));
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}
